package cn.ebook.jdbc.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.naming.NamingException;

import cn.ebook.entity.Category;
import cn.ebook.jdbc.dao.CategoryDao;
import cn.ebook.util.BaseUtil;

public class CategoryImpl implements CategoryDao {
/**
 * 实现图书JNDI接口
 *@author 洋洋洋
 *@date 2018-7-25 4:45
 *细节：把异常抛给上一层》业务层
 **/
	//连接数据库实例
	private Connection conn;
	//预执行sql实例
	private PreparedStatement ps;
	//结果集实例
	private ResultSet rs;
	/**实现CategoryDao的寻找所有图书类型的方法
	 * @author 洋洋洋.
	 * @date 2018-7-25 8:27.
	 * @throws SQLException 
	 * @throws NamingException */
	public List<Category> findAllCategorys() throws SQLException, NamingException {
		
		List<Category>list=new ArrayList<Category>();
		//封装sql
		String sql="select id , category from ebook_category ";
		//获取连接
		conn=BaseUtil.getConnection();
		//预执行sql
		ps=conn.prepareStatement(sql);
		//执行sql返回结果
		rs=ps.executeQuery();
		//遍历结果集
		while(rs.next()){
			Category category=new Category();
			category.setId(rs.getLong("id"));
			category.setEbookCategory(rs.getString("category"));
			list.add(category);
		}
		//释放资源
		BaseUtil.closeAll(rs, conn, ps);
		//返回结果
		return list;
	}

}
